package com.walkthecow.justdraw;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;

public class PenSettings extends Activity implements SeekBar.OnSeekBarChangeListener, OnFocusChangeListener ,OnClickListener, RadioGroup.OnCheckedChangeListener {

	private Button buttonOK;
	private Button buttonCancel;
	private RadioButton radioButtonNoEffect;
	private RadioButton radioButtonBlur;
	private RadioButton radioButtonNeon;
	private RadioButton radioButtonEmboss;
	private SeekBar seekbarWidth;
	private EditText editTextWidth;
	private RadioGroup radioGroupEffect;
	private RadioButton radioShadow;

	private int mEffect = 0;
	private int mWidth = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.pen_settings_layout);
		Bundle extras = getIntent().getExtras();
		if(extras!=null){
			mEffect = extras.getInt(Constants.KEY_EFFECT);
			mWidth = extras.getInt(Constants.KEY_WIDTH);
		}

		buttonCancel = (Button) findViewById(R.id.pen_button_cancel);
		buttonOK = (Button) findViewById(R.id.pen_button_ok);
		radioGroupEffect = (RadioGroup) findViewById(R.id.pen_radiogroup_effects);
		radioButtonBlur = (RadioButton) findViewById(R.id.pen_radio_blur);
		radioButtonEmboss = (RadioButton) findViewById(R.id.pen_radio_emboss);
		radioButtonNeon = (RadioButton) findViewById(R.id.pen_radio_neon);
		radioButtonNoEffect = (RadioButton) findViewById(R.id.pen_radio_normal);
		seekbarWidth = (SeekBar) findViewById(R.id.pen_seekbar_width);
		editTextWidth = (EditText) findViewById(R.id.pen_edittext_width_value);
		radioShadow = (RadioButton) findViewById(R.id.pen_radio_shadow);

		seekbarWidth.setProgress(mWidth);
		editTextWidth.setText(Integer.toString(mWidth));

		switch(mEffect){
		case Constants.EFFECT_NONE:
			radioButtonNoEffect.setChecked(true);
			break;
		case Constants.EFFECT_BLUR:
			radioButtonBlur.setChecked(true);
			break;
		case Constants.EFFECT_EMBOSS:
			radioButtonEmboss.setChecked(true);
			break;
		case Constants.EFFECT_NEON:
			radioButtonNeon.setChecked(true);
			break;
		case Constants.EFFECT_SHADOW:
			radioShadow.setChecked(true);
		}

		buttonCancel.setOnClickListener(this);
		buttonOK.setOnClickListener(this);
		seekbarWidth.setOnSeekBarChangeListener(this);
		radioGroupEffect.setOnCheckedChangeListener(this);
		editTextWidth.setOnFocusChangeListener(this);

		buttonCancel.requestFocus();

	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// checkedId is the RadioButton selected
		switch(checkedId){
		case R.id.pen_radio_normal:
			mEffect = Constants.EFFECT_NONE;
			break;
		case R.id.pen_radio_blur:
			mEffect = Constants.EFFECT_BLUR;
			break;
		case R.id.pen_radio_emboss:
			mEffect = Constants.EFFECT_EMBOSS;
			break;
		case R.id.pen_radio_neon:
			mEffect = Constants.EFFECT_NEON;
			break;
		case R.id.pen_radio_shadow:
			mEffect = Constants.EFFECT_SHADOW;
			break;
		}
	}

	@Override
	public void onClick(View v) {

		Intent i = new Intent();

		switch(v.getId()){
		case R.id.pen_button_cancel:
			setResult(RESULT_CANCELED);
			finish();
			break;
		case R.id.pen_button_ok:
			i.putExtra(Constants.KEY_WIDTH, mWidth);
			i.putExtra(Constants.KEY_EFFECT, mEffect);
			setResult(RESULT_OK, i);
			finish();
			break;
		}
	}


	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
		editTextWidth.setText(Integer.toString(progress));
		mWidth = progress;
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		seekBar.setSecondaryProgress(seekBar.getProgress());

	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		

	}



	@Override
	public void onFocusChange(View v, boolean hasFocus) {

		if(v.getId()==R.id.pen_edittext_width_value){
			Resources res = getResources();
			int maxWidth = res.getInteger(R.integer.max_pen_width);

			if(!editTextWidth.getText().toString().equals("")){
				if(Integer.parseInt(editTextWidth.getText().toString()) > maxWidth){
					editTextWidth.setText(Integer.toString(maxWidth));
				}
				seekbarWidth.setProgress(Integer.parseInt(editTextWidth.getText().toString()));
				mWidth = seekbarWidth.getProgress();
			} else {
				seekbarWidth.setProgress(0);
				mWidth = 0;
			}	
		}

	}




}